<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="类型" prop="status">
        <el-select clearable v-model="queryParams.id" placeholder="请选择类型" @change="getList()">
          <el-option
            v-for="item in typeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="announcementList"
      @selection-change="handleSelectionChange"
      stripe
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="left" prop="title" width="500px" />
      <el-table-column label="内容" align="center" prop="content">
        <template slot-scope="scope">
          <el-popover popper-class="my-popper" trigger="click" placement="top">
            <div v-html="scope.row.content"></div>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium" class="pointer">查看详情</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column width="300px" label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag  type="success">{{scope.row.title}}</el-tag>
          <!-- <el-tag v-if="scope.row.id === 2"> 入园条件 </el-tag>
          <el-tag v-if="scope.row.id === 4" type="danger">机构设置</el-tag>
          <el-tag v-if="scope.row.id === 8" type="danger">地方政策法规</el-tag>
          <el-tag v-if="scope.row.id === 9" type="danger">国家政策法规</el-tag>
          <el-tag v-if="scope.row.id === 10" type="danger">
            知识产权法规
          </el-tag>
          <el-tag v-if="scope.row.id === 11" type="danger">
            项目申报法规
          </el-tag>
          <el-tag v-if="scope.row.id === 12" type="danger">
            中小企业申报信息
          </el-tag>
          <el-tag v-if="scope.row.id === 13" type="danger">
            桂林电子科技大学重点平台一览表
          </el-tag>
          <el-tag v-if="scope.row.id === 14" type="danger">
            科技园管理办法
          </el-tag>
          <el-tag v-if="scope.row.id === 15" type="danger">
            科技园众创空间管理办法
          </el-tag>
          <el-tag v-if="scope.row.id === 16" type="danger">
            科技园两帮九助创新创业服务管理办法
          </el-tag>
          <el-tag v-if="scope.row.id === 18" type="danger">
            科技型中小企业申报辅导
          </el-tag>

          <el-tag v-if="scope.row.id === 19" type="danger">
            高企申报辅导
          </el-tag>
          <el-tag v-if="scope.row.id === 20" type="danger"> 荣誉与资质 </el-tag>
          <el-tag v-if="scope.row.id === 21" type="danger"> 企业之窗 </el-tag>
          <el-tag v-if="scope.row.id === 22" type="danger">
            高新技术企业
          </el-tag>
          <el-tag v-if="scope.row.id === 23" type="danger"> 企业荣誉 </el-tag> -->
        </template>
      </el-table-column>

      <el-table-column
        width="200px"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:announcement:edit']"
          >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input disabled v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <web-editor v-model="form.content" :min-height="192" />
          <div style="font-size: 12px; color: #999; margin-top: 5px;">
            字数：{{ getContentLength }} / 10000
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnnouncement,
  getAnnouncement,
  delAnnouncement,
  addAnnouncement,
  updateAnnouncement,
} from "@/api/system/announcement";
import { getToken } from "@/utils/auth";
import { uploadFile } from "@/api/system/file.js";
import { updateAnnouncementStatus } from "../../../api/system/announcement";
import {
  listInformation,
  listIntroduction,
  updateIntroduction,
} from "../../../api/system/introduction";

export default {
  name: "Announcement",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      announcementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示审核弹出层
      openAudit: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 20,
        id: null,
        title: null,
        content: null,
        type: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              const isEmpty = !value || value.trim() === '' || value === '<p><br></p>';
              if (isEmpty) {
                return callback(new Error('请输入内容'));
              }
              const div = document.createElement('div');
              div.innerHTML = value;
              const textContent = div.textContent || div.innerText || '';
              if (textContent.length > 10000) {
                return callback(new Error('内容不能超过10000个字符'));
              }
              callback();
            },
            trigger: 'blur'
          }
        ],
      },
      fileList: [],
      uploadedFileList: [],
      typeOptions: [
        { value: 1, label: "园区简介" },
        { value: 2, label: "园区政策" },
        { value: 3, label: "科技园管理办法" },
        { value: 4, label: "科技园众创空间管理办法" },
        { value: 5, label: "科技园两帮九助创新创业服务管理办法" },
        { value: 6, label: "机构设置" },
        { value: 7, label: "众创空间入园条件" },
        { value: 8, label: "科技园入园条件"  },
        { value: 9, label: "荣誉与资质"  },
        { value: 10, label: "科技园入驻流程"  },
        { value: 11, label: "众创空间入园流程"  },



        // { value: 4, label: "机构设置" },
        // { value: 8, label: "地方政策法规" },
        // { value: 9, label: "国家政策法规" },
        // { value: 10, label: "知识产权法规" },
        // { value: 11, label: "项目申报法规" },
        // { value: 12, label: "中小企业申报信息" },
        // { value: 13, label: "桂林电子科技大学重点平台一览表" },
        // { value: 14, label: "科技园管理办法" },
        // { value: 15, label: "科技园众创空间管理办法" },
        // { value: 16, label: "科技园两帮九助创新创业服务管理办法" },
        // { value: 18, label: "科技型中小企业申报辅导" },
        // { value: 19, label: "高企申报辅导" },
        // { value: 20, label: "荣誉与资质" },
        // { value: 21, label: "企业之窗" },
        // { value: 22, label: "高新技术企业" },
        // { value: 23, label: "企业荣耀" },
        // { value: 24, label: "公共服务平台" },
        // { value: 25, label: "公共资源申请操作要求"}
      ],
      statusOptions: [
        { value: 0, label: "未审核" },
        { value: 1, label: "已审核" },
        { value: 2, label: "审核未通过" },
      ],
    };
  },
  computed: {
    getContentLength() {
      const content = this.form.content;
      if (!content) return 0;
      const div = document.createElement('div');
      div.innerHTML = content;
      const text = div.textContent || div.innerText || '';
      return text.length;
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getToken() {
      return getToken();
    },

    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listInformation(this.queryParams).then((response) => {
        this.announcementList = response.data.records.map((item) => {
          const typeOption = this.typeOptions.find(
            (option) => option.value === item.id
          );
          if (typeOption) {
            item.title = typeOption.label; // 添加 typeLabel 字段
          }
          return item;
        });
        console.log(this.announcementList);
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        content: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        files: null,
        type: null,
      };
      this.fileList = [];
      this.uploadedFileList = [];
      this.resetForm("form");
    },
    handleAudit(row) {
      this.auditForm.id = row.id;
      this.openAudit = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      (this.queryParams = {
        current: 1,
        size: 10,
        id: null,
        title: null,
        content: null,
        type: null,
        status: null,
      }),
        this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.reset();
      this.form = {
        id: row.id,
        content: row.content
      };
      this.open = true;
      this.title = "修改内容";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          // 再次检查内容长度
          const div = document.createElement('div');
          div.innerHTML = this.form.content;
          const textContent = div.textContent || div.innerText || '';
          if (this.form.content && textContent.length > 10000) {
            this.$message.error(`内容长度(${textContent.length})超过限制(10000字符)，请修改后重新提交`);
            return;
          }
          if (this.form.id != null) {
            updateIntroduction(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除所选的文章？")
        .then(function () {
          return delAnnouncement(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>.flex {
  max-width: 500px;
  text-wrap-mode: wrap;
  display: flex;
  .label {
    width: 40px;
  }
}
.bt {
  width: 100%;
}

.content-length-info {
  text-align: right;
  margin-top: 5px;
  font-size: 12px;
  color: #909399;
}

.content-length-info .over-limit {
  color: #f56c6c;
}
</style>



